package top.kpromise.fastdev.goods.controller.backend;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import top.kpromise.fastdev.goods.model.request.CreateGoodsModel;
import top.kpromise.fastdev.goods.response.Result;
import top.kpromise.fastdev.goods.service.GoodsService;
import top.kpromise.fastdev.goods.utils.StringUtils;

import java.util.Map;

@RestController
@RequestMapping("/admin/Goods")
public class GoodsController {

    private final GoodsService goodsService;

    public GoodsController(GoodsService goodsService) {
        this.goodsService = goodsService;
    }

    @RequestMapping("/create")
    @Transactional
    public Map<String, Object> createBySku(@RequestBody CreateGoodsModel goodsModel) {
        if (goodsModel.getBanners() == null || goodsModel.getBanners().isEmpty()) {
            return Result.error("请至少上传一张轮播图");
        }
        goodsService.createGoods(goodsModel);
        return Result.data("请求成功");
    }

    @RequestMapping("/goodsList")
    public Map<String, Object> goodsList(@RequestParam(value = "keyWord", required = false,
            defaultValue = "") String keyWord, int pageNo, int pageSize) {
        if (StringUtils.isEmpty(keyWord)) keyWord = null;
        return Result.data(goodsService.search(keyWord, pageNo, pageSize));
    }
}
